
local addonName, IWonew = ...

WayToolClass = {}
WayToolClass.__index = WayToolClass


function WayToolClass:new()
  local instance = setmetatable({}, WayToolClass)
	return instance
end

-- /dump WayToolClassSetTarget()

function WayToolClassSetTarget()

  -- 配置参数 x=0.44750779867172|y=0.56342256069183|face=1.9575946331024
  local TARGET_X = 0.44750779867172  -- 目标X坐标（0-1范围）
  local TARGET_Y = 0.56342256069183  -- 目标Y坐标（0-1范围）
  local TARGET_FACING = 1.9575946331024  -- 目标朝向（π=180°，0=正北）
  local DISTANCE_THRESHOLD = 0.01  -- 到达判定阈值（约10码）
  local FACING_THRESHOLD = 0.01  -- 朝向允许误差（弧度）

  -- 创建绿色方块
  local square = CreateFrame("Frame", nil, UIParent)
  square:SetSize(80, 80)  -- 方块大小
  square.texture = square:CreateTexture(nil, "OVERLAY")
  square.texture:SetColorTexture(0, 1, 0, 0.8)  -- 绿色，80%透明度
  square.texture:SetAllPoints()
  square:Hide()

  -- 将方块定位到玩家头顶
  local function UpdateSquarePosition()
      -- 使用玩家模型作为参考点
      local playerFrame = _G["PlayerFrame"]
      if playerFrame then
          square:ClearAllPoints()
          square:SetPoint("BOTTOM", playerFrame, "TOP", 0, 40)  -- 在玩家头顶上方40像素
      else
          -- 如果PlayerFrame不可用，使用默认位置
          square:ClearAllPoints()
          square:SetPoint("CENTER", UIParent, "CENTER", 0, 100)
      end
  end

  -- 检查玩家位置和朝向
  local function CheckPosition()
      local mapID = C_Map.GetBestMapForUnit("player")
      if not mapID then return end

      local pos = C_Map.GetPlayerMapPosition(mapID, "player")
      if not pos then return end

      local x, y = pos:GetXY()
      local distance = math.sqrt((x - TARGET_X)^2 + (y - TARGET_Y)^2)
      local currentFacing = GetPlayerFacing()

      -- 检查是否到达目标位置和朝向
      if distance < DISTANCE_THRESHOLD and
        (not TARGET_FACING or math.abs(currentFacing - TARGET_FACING) < FACING_THRESHOLD) then
          UpdateSquarePosition()
          square:Show()
      else
          square:Hide()
      end
  end

  -- 设置定时检查
  local frame = CreateFrame("Frame")
  frame:SetScript("OnUpdate", function(self, tss)
      CheckPosition()
  end)

  -- 初始更新位置
  UpdateSquarePosition()
end


-- /dump WayToolClassDebug()
function WayToolClassDebug()
  local map = C_Map.GetBestMapForUnit("player")
  if not map then return end
  local position = C_Map.GetPlayerMapPosition(map, "player")
  if not position then return end
  print(position:GetXY())
  local facing = GetPlayerFacing()
  print("facing", facing)
  local x, y = position:GetXY()
  print(2222222, x, y)
  IWonew.Log("x="..tostring(x).."|y="..tostring(y).."|face="..tostring(facing))
  -- 
  print(5555)
end


